package cn.hls.dao.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import cn.hls.dao.ShipOrderDao;
import cn.hls.entity.ShipOrder;
import cn.hls.entity.vo.PageInfo;
import cn.hls.exception.BussinessException;
import cn.hlsconstant.DataAssessEnum;
/**
  *@fileName: ShipOrderDaoImpl.java
  *@package: cn.hls.dao.impl
　*@Des
iption:　托运运单数据操作接口实现类
　*@author: yinghui.zhang
　*@eimport com.sun.xml.internal.messaging.saaj.packaging.mime.internet.ParameterList;
mail: uuhui@163.com
  *@date: 2016年6月10日
 *
 */
@Component("shipOrderDao")
public class ShipOrderDaoImpl extends BaseDaoImpl<ShipOrder> implements ShipOrderDao {
	@Override
	public ShipOrder find(Serializable entityId) throws Exception {
		String selectSql="from ShipOrder p inner join fetch p.shipper sp inner join fetch p.car c inner join fetch c.carCategory cc where p.deleted=? and p.id=? ";
		List<ShipOrder>list= findByHql(selectSql, new Object[]{false,entityId});
		if(list!=null && !list.isEmpty()){
			return list.get(0);
		}else{
			return null;
		}
	}
	@Override
	public PageInfo<ShipOrder> loadShipOrderInfo(ShipOrder entity, PageInfo<ShipOrder> page) throws Exception {
		// 定义查询Hql语句
		StringBuilder selectHql=new StringBuilder("from ShipOrder p inner join fetch p.shipper sh inner join fetch sh.company com inner join fetch p.car c inner join fetch c.carCategory cc  ");
		// 定义汇总Hql语句
		StringBuilder counthql = new StringBuilder("select count(p.id) from ShipOrder p  inner join  p.shipper sh inner join sh.company com inner join  p.car c inner join  c.carCategory cc ");
		//定义查询条件hql
		StringBuilder condHql=new StringBuilder(" where p.deleted=? ");
		// 定义查询参数
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(false);
		if (entity != null) {
			//按电子运单编号查询
			if(entity.getId()!=null){
				condHql.append("and p.id=? ");
				paramList.add(entity.getId());
			}
			//按电子运单的承运企业编号查询
			if(entity.getCompanyId()!=null){
				condHql.append("and p.companyId=? ");
				paramList.add(entity.getCompanyId());
			}
			//按车辆信息查询
			if(entity.getCar()!=null){
				//按车牌号查询
				if(StringUtils.hasText(entity.getCar().getLicenseNum())){
					condHql.append("and c.licenseNum like ? ");
					paramList.add("%"+entity.getCar().getLicenseNum().trim()+"%");
				}
				if(StringUtils.hasText(entity.getCar().getLicenseNum2())){
					condHql.append("and c.licenseNum2 like ? ");
					paramList.add("%"+entity.getCar().getLicenseNum2()+"%"	);
				}
			}
			//按驾驶员查询
			if(StringUtils.hasText(entity.getDriverName())){
				condHql.append("and p.driverName like ? ");
				paramList.add("%"+entity.getDriverName().trim()+"%");
			}
			//按押运员查询
			if(StringUtils.hasText(entity.getEscortName())){
				condHql.append("and p.escortName like ? ");
				paramList.add("%"+entity.getEscortName().trim()+"%");
			}
			//按托运日期查询
			if(entity.getBeginShipTime()!=null){
				condHql.append("and p.shipTime>=? ");
				paramList.add(entity.getBeginShipTime());
			}
			if(entity.getEndShipTime()!=null){
				condHql.append("and p.shipTime<=? ");
				paramList.add(entity.getEndShipTime());
			}
		}
		counthql. append(condHql);
		selectHql.append(condHql);
		selectHql.append(" order by p.id desc ");
		return find(selectHql.toString(), counthql.toString(), paramList.toArray(), page);
	}
	
	@Override
	public List<ShipOrder> loadShipOrderList(ShipOrder entity)throws Exception{
		// 定义查询Hql语句
		StringBuilder selectHql=new StringBuilder("from ShipOrder p where p.deleted=? ");
		// 定义查询参数
		List<Object> paramList = new ArrayList<Object>();
		paramList.add(false);
		if (entity != null) {
			//按车辆信息查询
			if(entity.getCar()!=null){
				//按车牌号查询
				if(StringUtils.hasText(entity.getCar().getLicenseNum())){
					selectHql.append("and c.licenseNum like ? ");
					paramList.add("%"+entity.getCar().getLicenseNum().trim()+"%");
				}
				if(StringUtils.hasText(entity.getCar().getLicenseNum2())){
					selectHql.append("and c.licenseNum2 like ? ");
					paramList.add("%"+entity.getCar().getLicenseNum2()+"%"	);
				}
			}
			//按驾驶员查询
			if(StringUtils.hasText(entity.getDriverName())){
				selectHql.append("and p.driverName like ? ");
				paramList.add("%"+entity.getDriverName().trim()+"%");
			}
			//按押运员查询
			if(StringUtils.hasText(entity.getEscortName())){
				selectHql.append("and p.escortName like ? ");
				paramList.add("%"+entity.getEscortName().trim()+"%");
			}
			//按托运日期查询
			if(entity.getBeginShipTime()!=null){
				selectHql.append("and p.shipTime>=? ");
				paramList.add(entity.getBeginShipTime());
			}
			if(entity.getEndShipTime()!=null){
				selectHql.append("and p.shipTime<=? ");
				paramList.add(entity.getEndShipTime());
			}
		}
		return findByHql(selectHql.toString(), paramList.toArray());
	}
	
	@Override
	public ShipOrder getShipOrderBycar(String licenseNum,String licenseNum2)throws Exception{
		// 定义查询Hql语句
		StringBuilder hql=new StringBuilder("from ShipOrder p inner join fetch p.shipper sh inner join fetch sh.company com inner join fetch p.car c inner join fetch c.carCategory cc where p.deleted=? ");
		// 定义查询参数
		List<Object>params=new ArrayList<Object>();
		params.add(false);
		if(StringUtils.hasText(licenseNum)){
			hql.append("and c.licenseNum=? ");
			params.add(licenseNum.trim());
		}
		if(StringUtils.hasText(licenseNum2)){
			hql.append("and c.licenseNum2=? ");
			params.add(licenseNum2.trim());
		}
		hql.append("order by p.id desc ");
	    List<ShipOrder>	list=findByHql(hql.toString(),params.toArray());
		if(list!=null && !list.isEmpty()){
			return list.get(0);
		}else{
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Long> loadShipOrderIdList(ShipOrder entity)throws Exception{
		StringBuilder selectHql=new StringBuilder("select id from ShipOrder p where deleted=?  ");
		List<Object>paramList=new ArrayList<Object>();
		paramList.add(false);
		if(entity!=null){
			//按车牌号查询
			if(StringUtils.hasText(entity.getCar().getLicenseNum())){
				selectHql.append("and p.car.licenseNum=? ");
				paramList.add(entity.getCar().getLicenseNum().trim());
			}
			if(StringUtils.hasText(entity.getCar().getLicenseNum2())){
				selectHql.append("and p.car.licenseNum2=? ");
				paramList.add(entity.getCar().getLicenseNum2().trim()	);
			}
		}
		//托运单日期
		if(entity.getShipTime()!=null){
			selectHql.append("and p.shipTime=? ");
			paramList.add(entity.getShipTime());
		}
		
		Query query = getSession().createQuery(selectHql.toString());
		if (paramList != null) {
			for (int i = 0; i < paramList.size(); i++) {
				query.setParameter(i, paramList.get(i));
			}
		}
		return query.list();
	}

	@Override
	public void delete(Object entityId) throws Exception {
		String []ids=(String[])entityId;
		StringBuilder hql=new StringBuilder("update ShipOrder set deleted=? where id in (");
		List<Object>params=new ArrayList<Object>();
		params.add(true);
		for (String id:ids){
			hql.append("?,");
			params.add(Long.valueOf(id));
		}	
		hql.delete(hql.length()-1, hql.length());
		hql.append(")");
		 try {
			executeHQLUpdate(hql.toString(), params.toArray());
		} catch (Exception e) {
			throw new BussinessException(DataAssessEnum.UPDATE_ERROR,"删除托运运单信息失败" );
		}
	}

	
	@Override
		public boolean exists(ShipOrder entity)throws Exception{
			StringBuilder selectHql=new StringBuilder("select count(p.id) from ShipOrder p where p.deleted=? ");
			List<Object>paramList=new ArrayList<Object>();
			paramList.add(false);
			if(entity!=null){
				//按车辆信息查询
				if(entity.getCar()!=null){
					//按车牌号查询
					if(StringUtils.hasText(entity.getCar().getLicenseNum())){
						selectHql.append("and p.car.licenseNum=? ");
						paramList.add(entity.getCar().getLicenseNum().trim());
					}
					if(StringUtils.hasText(entity.getCar().getLicenseNum2())){
						selectHql.append("and p.car.licenseNum2=? ");
						paramList.add(entity.getCar().getLicenseNum2().trim()	);
					}
				}
				//托运单日期
				if(entity.getShipTime()!=null){
					selectHql.append("and p.shipTime=? ");
					paramList.add(entity.getShipTime());
				}
				
			}
			Query query=this.getSession().createQuery(selectHql.toString());
			for(int i=0;i<paramList.size();i++){
				query.setParameter(i, paramList.get(i));
			}
			Long c = Long.valueOf(((Number) query.uniqueResult()).longValue());
			return c.intValue()>0?true:false;
	}
	
}
